package teaching.util;
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import java.util.Date;
import java.util.Map;

/**
 * Author:20201002983_郑雨蓓
 * Date: 2024/3/2
 * Description:
 */
public class JwtUtil {
    private static final String KEY = "sodacat";//指定密钥配置算法

    public static String getToken(Map<String, Object> claims) {
        return JWT.create()
                .withClaim("claims", claims)
                .withExpiresAt(new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 24))//添加过期时间1000是一秒，设置24小时过期
                .sign(Algorithm.HMAC256(KEY));
    }

    public static Map<String, Object> parseToken(String token) {
        return JWT.require(Algorithm.HMAC256(KEY))
                .build()
                .verify(token)//验证token生成解析后的JWT对象
                .getClaim("claims")
                .asMap();
    }

}
